package view;

import model.JavaFilter;
import com.mysql.jdbc.Connection;
import controller.CinemaController;
import java.io.File;
import java.sql.DriverManager;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import model.FilmesModel;
import model.Relatorio;
import model.SalasModel;
import model.SessoesModel;

/**
 * @author Lucas Amaral
 */
public class CinemaView extends javax.swing.JFrame {
    public Connection c;
    private SalasModel modelSala;
    private SessoesModel modelSessão;
    private FilmesModel modelFilme;
    private CinemaController controller;
    
    public CinemaView() {
        initComponents();
        conectar();
        modelSala = new SalasModel(c);
        modelFilme = new FilmesModel(c);
        modelSessão = new SessoesModel(c, modelSala.getSalas(), modelFilme.getFilmes());
        controller = new CinemaController(this, modelSala, modelSessão, modelFilme);
    }
                
    public void conectar() {
        try {
            String url = "jdbc:mysql://localhost:3306/cinema?user=root&password=root";
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            c = (Connection) DriverManager.getConnection(url);
            System.out.println("Conectado");
        } catch (Exception e) {/**/}
    }
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane = new javax.swing.JScrollPane();
        jTableTabelaResultado = new javax.swing.JTable();
        jButtonEditar = new javax.swing.JButton();
        jButtonExcluir = new javax.swing.JButton();
        jMenuBarMenuPrincipal = new javax.swing.JMenuBar();
        jMenuConsultas = new javax.swing.JMenu();
        jMenuSalas = new javax.swing.JMenuItem();
        jMenuSessoes = new javax.swing.JMenuItem();
        jMenuItemFilmes = new javax.swing.JMenuItem();
        jMenuCadastros = new javax.swing.JMenu();
        jMenuSala = new javax.swing.JMenuItem();
        jMenuItemSessao = new javax.swing.JMenuItem();
        jMenuItemFilme = new javax.swing.JMenuItem();
        jMenuItemVendaIngresso = new javax.swing.JMenuItem();
        jMenuEmissao = new javax.swing.JMenu();
        jMenuRelatorioVendas = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jTableTabelaResultado.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jScrollPane.setViewportView(jTableTabelaResultado);

        jButtonEditar.setText("Editar");
        jButtonEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonEditarActionPerformed(evt);
            }
        });

        jButtonExcluir.setText("Excluir");
        jButtonExcluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonExcluirActionPerformed(evt);
            }
        });

        jMenuConsultas.setText("Consultar");

        jMenuSalas.setText("Salas");
        jMenuSalas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuSalasActionPerformed(evt);
            }
        });
        jMenuConsultas.add(jMenuSalas);

        jMenuSessoes.setText("Sessões");
        jMenuSessoes.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuSessoesActionPerformed(evt);
            }
        });
        jMenuConsultas.add(jMenuSessoes);

        jMenuItemFilmes.setText("Filmes");
        jMenuItemFilmes.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemFilmesActionPerformed(evt);
            }
        });
        jMenuConsultas.add(jMenuItemFilmes);

        jMenuBarMenuPrincipal.add(jMenuConsultas);

        jMenuCadastros.setText("Cadastrar");

        jMenuSala.setText("Sala");
        jMenuSala.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuSalaActionPerformed(evt);
            }
        });
        jMenuCadastros.add(jMenuSala);

        jMenuItemSessao.setText("Sessão");
        jMenuItemSessao.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemSessaoActionPerformed(evt);
            }
        });
        jMenuCadastros.add(jMenuItemSessao);

        jMenuItemFilme.setText("Filme");
        jMenuItemFilme.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemFilmeActionPerformed(evt);
            }
        });
        jMenuCadastros.add(jMenuItemFilme);

        jMenuItemVendaIngresso.setText("Venda de ingresso");
        jMenuItemVendaIngresso.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemVendaIngressoActionPerformed(evt);
            }
        });
        jMenuCadastros.add(jMenuItemVendaIngresso);

        jMenuBarMenuPrincipal.add(jMenuCadastros);

        jMenuEmissao.setText("Emitir");

        jMenuRelatorioVendas.setText("Relatório de vendas de ingresso");
        jMenuRelatorioVendas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuRelatorioVendasActionPerformed(evt);
            }
        });
        jMenuEmissao.add(jMenuRelatorioVendas);

        jMenuBarMenuPrincipal.add(jMenuEmissao);

        setJMenuBar(jMenuBarMenuPrincipal);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 959, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(245, 245, 245)
                .addComponent(jButtonEditar)
                .addGap(225, 225, 225)
                .addComponent(jButtonExcluir)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 349, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 21, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonEditar)
                    .addComponent(jButtonExcluir))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jMenuSalasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuSalasActionPerformed
        controller.consultarSalas();
    }//GEN-LAST:event_jMenuSalasActionPerformed
    private void jMenuSessoesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuSessoesActionPerformed
        controller.consultarSessoes();
    }//GEN-LAST:event_jMenuSessoesActionPerformed

    private void jMenuSalaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuSalaActionPerformed
        SalaView salaView = new SalaView(modelSala, null);
        salaView.setVisible(true);
    }//GEN-LAST:event_jMenuSalaActionPerformed

    private void jMenuItemSessaoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemSessaoActionPerformed
        SessaoView sessaoView = new SessaoView(modelSessão, null);
        sessaoView.setVisible(true);
    }//GEN-LAST:event_jMenuItemSessaoActionPerformed

    private void jMenuItemFilmesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemFilmesActionPerformed
        controller.consultarFilmes();
    }//GEN-LAST:event_jMenuItemFilmesActionPerformed

    private void jMenuItemFilmeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemFilmeActionPerformed
        FilmeView filmeView = new FilmeView(modelFilme, null);
        filmeView.setVisible(true);
    }//GEN-LAST:event_jMenuItemFilmeActionPerformed

    private void jMenuItemVendaIngressoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemVendaIngressoActionPerformed
        VendaIngressoView vendaView = new VendaIngressoView(modelSessão);
        vendaView.setVisible(true);
    }//GEN-LAST:event_jMenuItemVendaIngressoActionPerformed

    private void jMenuRelatorioVendasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuRelatorioVendasActionPerformed
    JavaFilter fJavaFilter = new JavaFilter ();
    File fFile = new File ("Relatório.csv");
    File file = null;
     JFileChooser fc = new JFileChooser ();

     // Start in current directory
     fc.setCurrentDirectory (new File ("."));

     // Set filter for Java source files.
     fc.setFileFilter (fJavaFilter);

     // Set to a default name for save.
     fc.setSelectedFile (fFile);

     // Open chooser dialog
     int result = fc.showSaveDialog (this);

     if (result == JFileChooser.CANCEL_OPTION) {
         return;
     } else if (result == JFileChooser.APPROVE_OPTION) {
         fFile = fc.getSelectedFile ();
         if (fFile.exists ()) {
             int response = JOptionPane.showConfirmDialog (null,
               "Sobrescrever o arquivo existente?","Confirmação de Sobrescrição",
                JOptionPane.OK_CANCEL_OPTION,
                JOptionPane.QUESTION_MESSAGE);
             if (response == JOptionPane.CANCEL_OPTION){ return;
             }
         }
         Relatorio relatorio = new Relatorio(modelSessão.getSessões());
         relatorio.writeFile (fFile, relatorio.createCsvFile());
         JOptionPane.showMessageDialog(this, "Relatório gerado com sucesso!");
     } else {
       return;
     }
    }//GEN-LAST:event_jMenuRelatorioVendasActionPerformed

    private void jButtonExcluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonExcluirActionPerformed
        try {
            controller.excluir(jTableTabelaResultado.getValueAt(jTableTabelaResultado.getSelectedRow(), 0));
        } catch(ArrayIndexOutOfBoundsException e) {
            JOptionPane.showMessageDialog(this, "Por favor, selecione a linha que deseja editar");        
        }
    }//GEN-LAST:event_jButtonExcluirActionPerformed

    private void jButtonEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonEditarActionPerformed
        try {
            controller.alterar(jTableTabelaResultado.getValueAt(jTableTabelaResultado.getSelectedRow(), 0));
        } catch(ArrayIndexOutOfBoundsException e) {
            JOptionPane.showMessageDialog(this, "Por favor, selecione a linha que deseja editar");        
        }
    }//GEN-LAST:event_jButtonEditarActionPerformed

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new CinemaView().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonEditar;
    private javax.swing.JButton jButtonExcluir;
    private javax.swing.JMenuBar jMenuBarMenuPrincipal;
    private javax.swing.JMenu jMenuCadastros;
    public javax.swing.JMenu jMenuConsultas;
    private javax.swing.JMenu jMenuEmissao;
    private javax.swing.JMenuItem jMenuItemFilme;
    private javax.swing.JMenuItem jMenuItemFilmes;
    private javax.swing.JMenuItem jMenuItemSessao;
    private javax.swing.JMenuItem jMenuItemVendaIngresso;
    private javax.swing.JMenuItem jMenuRelatorioVendas;
    private javax.swing.JMenuItem jMenuSala;
    private javax.swing.JMenuItem jMenuSalas;
    private javax.swing.JMenuItem jMenuSessoes;
    private javax.swing.JScrollPane jScrollPane;
    public javax.swing.JTable jTableTabelaResultado;
    // End of variables declaration//GEN-END:variables
}
